package jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class JDBCDemo12 {
    public static void main(String[] args) {
        try (Connection connection = DBUtil.getConnection()){
            String sql = "INSERT INTO userinfo (username,password,nickname,age) VALUES (?,?,?,?)";
            connection.setAutoCommit(false);
            connection.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
//        connection.setTransactionIsolation(Connection.TRANSACTION_REPEATABLE_READ);
//        connection.setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
//        connection.setTransactionIsolation(Connection.TRANSACTION_READ_UNCOMMITTED);
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,"艾斯奥特曼");
            preparedStatement.setString(2,"90908080");
            preparedStatement.setString(3,"艾斯");
            preparedStatement.setInt(4,678);
            int num = preparedStatement.executeUpdate();
            System.out.println(num>0?"插入成功":"插入失败");
            connection.commit();
            System.out.println("事务提交成功");
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
